package com.rimi.project.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.rimi.project.bean.UserCard;
import com.rimi.project.dao.UserCardDao;
import com.rimi.project.util.JDBCutil;

public class UserCardDaoImpl implements UserCardDao{

	// 添加账号密码
	@Override
	public boolean insertCard(int card, String username, String parkingPlace) {
		String sql = "insert into user_parking values(?,?,?)";
		
		int row = JDBCutil.preUpdate(sql, new Object[] {card,username,parkingPlace});
		
		return row == 1;
	}

	@Override
	public List<UserCard> getAllCard() {
		List<UserCard> cards = new ArrayList<>();
		
		String sql = "select * from user_parking";
		
		ResultSet resultSet = JDBCutil.preQuery(sql, null);
		
		try {
			while (resultSet.next()) {
				// 取出一本图书
				UserCard card = new UserCard(resultSet.getInt("card"), 
						resultSet.getString("username"), 
						resultSet.getString("parkingPlace"),
						resultSet.getTimestamp("startTime"));
				
				// 将构造图书加入图书列表
				cards.add(card);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return cards;
	}

}
